<?php


namespace app\api\controller;


//yr.com/api/login/test
use app\api\service\Login as loginService;
use app\api\validate\LoginValidate;
use think\Db;
use think\Loader;
use app\api\service\Message as messageService;

class Login extends BaseApi {

    #小程序登录
    public function login(){

        $loginValidate = new LoginValidate();
        $validateRes = $loginValidate->goSceneCheck('login');
        if(is_array($validateRes) && array_key_exists('code',$validateRes)){
        	return error($validateRes['msg']);
        }

        $code = $this->request->param('code');

        $loginService = new loginService();
        $res = $loginService->getOpenid($code);
        return $res;
    }

    #更新用户信息
    public function saveUserInfo(){
        Loader::import('encrypted/wxBizDataCrypt',EXTEND_PATH);
	    $loginValidate = new LoginValidate();
	    $validateRes = $loginValidate->goSceneCheck('saveUserInfo');
	    if(is_array($validateRes) && array_key_exists('code',$validateRes)){
		    return error($validateRes['msg']);
	    }

        $encryptedData = $this->request->param('data');
        $iv = $this->request->param('iv');
        $sessionKey = $this->request->param('sessionKey');

        $pc = new \WXBizDataCrypt(Config('wxinfo.appId'), $sessionKey);
        $errCode = $pc->decryptData($encryptedData,$iv,$data);
        if($errCode == 0){
            $result = json_decode($data,true);
            $member = new Member();
            $item = $member->getMember($result['openId']);
            if(!empty($item) && empty($item->avatar_image)){
                $item->nickname = $result['nickName'];
                $item->province = $result['province'];
                $item->country = $result['country'];
                $item->city = $result['city'];
                $item->avatar_image = $result['avatarUrl'];
                $item->unionId = $result['unionId'];
                $item->save();
            }
            return success($item);
        }else{
        	return error('用户信息更新失败');
        }
    }

    //验证token有效性
    public function verifyToken(){

	    $loginValidate = new LoginValidate();
	    $validateRes = $loginValidate->goSceneCheck('verifyToken');
	    if(is_array($validateRes) && array_key_exists('code',$validateRes)){
		    return error($validateRes['msg']);
	    }

        $token = trim($this->request->param('token'));
        $accessTokenService = new accessTokenService();
        $item = $accessTokenService::checkToken($token);
        return json($item);
    }

    //获取电话号码
    public function getPhoneNumber(){
        Loader::import('encrypted/wxBizDataCrypt',EXTEND_PATH);

	    $loginValidate = new LoginValidate();
	    $validateRes = $loginValidate->goSceneCheck('saveUserInfo');
	    if(is_array($validateRes) && array_key_exists('code',$validateRes)){
		    return error($validateRes['msg']);
	    }

	    $encryptedData = $this->request->param('data');
	    $iv = $this->request->param('iv');
	    $sessionKey = $this->request->param('sessionKey');

	    $pc = new \WXBizDataCrypt(Config('wxinfo.appId'), $sessionKey);
        $errCode = $pc->decryptData($encryptedData,$iv,$data);
        if($errCode == 0){
            $result = json_decode($data,true);
            return success(['mobile'=>$result['phoneNumber']]);
        }else{
        	return error('获取电话号码失败');
        }
    }

    /**
     * 发送短信
     * @return \think\response\Json
     */
    public function messaging(){
        $type = $this->request->param('type');
        //能获取到接受者手机号的即可,或者直接传入手机号
        $openid = $this->request->param('openid');

        $smsService = new smsService();
        if($type == 1){ //聚合短信
            $tpl_value = '#code#=1234&#company#=聚合数据';//您设置的模板变量，根据实际情况修改
            $item = $smsService->jhSms($openid,$tpl_value);
        }else{
            $code=get_random(3);
            $params = array($code,'30'); //模板参数列表，如模板 {1}...{2}...{3}，那么需要带三个参数
            $item = $smsService->TencentSms($openid,$params);
        }
        return json($item);
    }


    public function test(){
    	return error('成功',1);
    }



}